package br.com.arndroid.coupman.site;

import android.widget.EditText;
import br.com.arndroid.lib.entity.EntityFormActivity;
import br.com.arndroid.lib.validation.ConstraintViolations;
import br.com.arndroid.coupman.R;

public class SiteFormActivity extends EntityFormActivity {
	
	private static SiteController globalController;

	//Set this for mock controller define (or null to undefine):
	public static void setGlobalController(SiteController controller) {
		globalController = controller;
	}
	
	//Controller:
	SiteController mController;
	
	//VO:
	SitePOJO mVO;
	
	//Fields:
	EditText mEdtSiteName, mEdtSiteURL;
	
	@Override
	public void onBindUI() {
		mController = globalController == null ? SiteController.getInstance(this) : globalController;
		mVO = new SitePOJO();
		mEdtSiteName = (EditText) findViewById(R.id.edtSiteName);
		mEdtSiteURL = (EditText) findViewById(R.id.edtSiteURL);
	}

	@Override
	public int onAcquireLayoutResID() {
		return R.layout.site_dlg;
	}

	@Override
	public int onAcquireNegativeButtonResID() {
		return R.id.btnCancel;
	}

	@Override
	public int onAcquirePositiveButtonResID() {
		return R.id.btnOK;
	}

	@Override
	public void onInitUI(String currentAction) {
		if(EntityFormActivity.ENTITY_ACTION_UPDATE.equals(currentAction)) {
			SitePOJO site = mController.getById(getEntityID());
			mEdtSiteName.setText(site.name);
			mEdtSiteURL.setText(site.url_uri);
		}
	}

	@Override
	public long onPersistUI(String currentAction) {
		bindVO(mEdtSiteName.getText().toString(), mEdtSiteURL.getText().toString());
		return mController.persist(mVO);
	}

	@Override
	public ConstraintViolations onValidateUI(String currentAction) {
		bindVO(mEdtSiteName.getText().toString(), mEdtSiteURL.getText().toString());
		return mController.validate(mVO);
	}
	
	private void bindVO(String name, String url) {
		//TODO: there are incomplete tests -> the line above was inserted but test NOT broken.
		mVO._id = ENTITY_ACTION_NEW.equals(getCurrentAction()) ? null : getEntityID();
		mVO.name = name.trim();
		mVO.url_uri = url.trim();
	}

	@Override
	public String onTranslateErrorMessage(String errorMessage) {
		if(SiteController.ERR_SITE_INVALID_NAME.equals(errorMessage)) {
			return getResources().getText(R.string.site_invalid_name).toString();
		} else if(SiteController.ERR_SITE_DUPLICATED_NAME.equals(errorMessage)) {
			return getResources().getText(R.string.site_duplicated_name).toString();
		} else if(SiteController.ERR_SITE_INVALID_URL.equals(errorMessage)) {
			return getResources().getText(R.string.site_invalid_url).toString();
		} else {
			throw new IllegalArgumentException("Invalid errorMessage");
		}
	}

	@Override
	public String onAcquireOperationTitle(String currentAction) {
		return currentAction.equals(ENTITY_ACTION_NEW) ?
				getResources().getText(R.string.site_dlg_title_new).toString() :
				getResources().getText(R.string.site_dlg_title_update).toString();
	}

	@Override
	public String onAcquireValidationErrorTitle() {
		return getResources().getString(R.string.validation_error_title);
	}

	@Override
	public String onAcquireValidationErrorMessagePrefix() {
		return getResources().getString(R.string.validation_error_message_prefix);
	}
}
